<?php
/**
 * @Description 
 * @Sonkwl Xiong
 * @Date 2022/08/09 14:21:07
 */
class autoconfig{
    static private $sql=array();
    //返回sql
    static public function GetSql(){
        return self::$sql;
    }
    //配置属性
    static public function SetMysqlType($type){
        $sub=explode(":",$type);
        if(is_array($sub) && count($sub)==3){
            return 'varchar('.$sub[2].')';
        }else{
            return $type;
        }
    }
    //自动配置DB
    static public function SetMysql($tables){
        foreach ($tables as $num => $table) {
            self::$sql[]="drop table ".$table["table"].";";
            $sql="create table ".$table["table"]."(";
            foreach ($table["column"] as $key => $value) {
                $sql.=$key.' '.self::SetMysqlType($value["type"]).(isset($value["index"])?' '.$value["index"]:"").',';
            }
            $sql=trim($sql,",");
            $sql.=')charset=utf8;';
            self::$sql[]=$sql;
        }
        return __CLASS__;
    }
    //自动配置php-model
    static public function SetClass($tables){
        $time=date("Y-m-d H:i");
        if(!is_dir("model")){
            mkdir("model",0777);
        }
        if(!is_dir("model/php")){
            mkdir("model/php",0777);
        }
        foreach ($tables as $num => $table) {
            $classfile=fopen("model/php/".$table["class"].".php","w");
            fwrite($classfile,"<?php\n");
            fwrite($classfile,"//生成时间".$time."\n");
            fwrite($classfile,'class '.$table["class"]."{\n");
            $columns='';
            $values='';
            $updates='';
            $where='';
            $resetcode='';
            foreach ($table["column"] as $key => $value) {
                $desc=$value["desc"];
                fwrite($classfile,"    static private $".$key."=NULL;//".$value["desc"]."\n");
                fwrite($classfile,"    static public function Set_".$key."($".$key."){\n");
                //字数比对
                if(strstr($value["type"],"len")){
                    $sub=explode(":",$value["type"]);
                    fwrite($classfile,"        if(strlen($".$key.")<".$sub[1]." || strlen($".$key.")>".$sub[2]."){\n");
                    fwrite($classfile,"            return \"".$desc."的输入字数不在指定范围".$sub[1]."~".$sub[2]."\";\n");
                    fwrite($classfile,"        }\n");
                }
                //范围比对
                if(isset($value["area"])){
                    fwrite($classfile,"        if(!in_array($".$key.",array(\"".implode('","',$value["area"])."\"))){\n");
                    fwrite($classfile,"            return \"".$desc."的输入必须是[".implode(',',$value["area"])."]\";\n");
                    fwrite($classfile,"        }\n");
                }
                //正则比对
                if(isset($value["regex"])){
                    fwrite($classfile,"        if(!preg_match(\"".$value["regex"]."\",$".$key.")){\n");
                    // fwrite($classfile,"            return 103;\n");
                    fwrite($classfile,"            return \"".$desc."的输入格式错误\";\n");
                    fwrite($classfile,"        }\n");
                }
                //数字比对
                if(strstr($value["type"],"int")){
                    $sub=explode(":",$value["type"]);
                    fwrite($classfile,"        if(!is_numeric($".$key.")){\n");
                    // fwrite($classfile,"            return 104;\n");
                    fwrite($classfile,"            return \"".$desc."的输入必须是数字\";\n");
                    fwrite($classfile,"        }\n");
                }
                fwrite($classfile,"        self::$".$key."=addslashes($".$key.");\n");
                fwrite($classfile,"        return 1;\n");
                fwrite($classfile,"    }\n");
                $columns.="        \$sql.=self::$".$key."!=NULL?\"".$key.",\":'';\n";
                $values.="        \$sql.=self::$".$key."!=NULL?\"'\".self::$".$key.".\"',\":'';\n";
                if(isset($value["primary"]) || isset($value["fk"])){
                    $where.="        \$sql.=self::$".$key."!=NULL?\" and ".$key."='\".self::$".$key.".\"'\":'';\n";
                }else{
                    $updates.="        \$sql.=self::$".$key."!=NULL?\"".$key."='\".self::$".$key.".\"',\":'';\n";
                }
                $resetcode="        self::$".$key."=NULL;\n";
            }
            //Reset
            fwrite($classfile,"    static public function Reset(){\n");
            fwrite($classfile,$resetcode);
            fwrite($classfile,"    }\n");
            //ADD
            fwrite($classfile,"    static public function Add(){\n");
            fwrite($classfile,"        \$sql=\"insert into ".$table["table"]."(\";\n");
            fwrite($classfile,$columns);
            fwrite($classfile,"        \$sql=trim(\$sql,',');\n");
            fwrite($classfile,"        \$sql.=\") values (\";\n");
            fwrite($classfile,$values);
            fwrite($classfile,"        \$sql=trim(\$sql,',');\n");
            fwrite($classfile,"        \$sql.=\");\";\n");
            fwrite($classfile,"        return \$sql;\n");
            fwrite($classfile,"    }\n");
            //EDIT
            fwrite($classfile,"    static public function Edit(){\n");
            fwrite($classfile,"        \$sql=\"update ".$table["table"]." set \";\n");
            fwrite($classfile,$updates);
            fwrite($classfile,"        \$sql=trim(\$sql,',');\n");
            fwrite($classfile,"        \$sql.=\" where 1=1\";\n");
            fwrite($classfile,$where);
            fwrite($classfile,"        return \$sql.';';\n");
            fwrite($classfile,"    }\n");
            //DELETE
            fwrite($classfile,"    static public function Delete(){\n");
            fwrite($classfile,"        \$sql=\"delete from ".$table["table"]."\";\n");
            fwrite($classfile,"        \$sql.=\" where 1=1\";\n");
            fwrite($classfile,$where);
            fwrite($classfile,"        return \$sql.';';\n");
            fwrite($classfile,"    }\n");
            //SELECT
            fwrite($classfile,"    static public function Select(){\n");
            fwrite($classfile,"        \$sql=\"select * from ".$table["table"]."\";\n");
            fwrite($classfile,"        return \$sql;\n");
            fwrite($classfile,"    }\n");
            //finish
            fwrite($classfile,"}\n");
            fwrite($classfile,"?>");
            fclose($classfile);
        }
        return __CLASS__;
    }
    //自动配置js-model
    static public function SetJs($tables){
        $time=date("Y-m-d H:i");
        if(!is_dir("model")){
            mkdir("model",0777);
        }
        if(!is_dir("model/js")){
            mkdir("model/js",0777);
        }
        foreach ($tables as $num => $table) {
            $classfile=fopen("model/js/".$table["class"].".js","w");
            fwrite($classfile,"//生成时间:".$time."\n");
            fwrite($classfile,"var ".$table["class"]."={\n");
            foreach($table["column"] as $key => $value){
                fwrite($classfile,"    ".$key.":null,//".$value["desc"]."\n");
                fwrite($classfile,"    Set_".$key.":function(".$key."){\n");
                //字数比对
                if(strstr($value["type"],"len")){
                    $sub=explode(":",$value["type"]);
                    fwrite($classfile,"        if(".$key.".length<".$sub[1]." || ".$key.".length>".$sub[2]."){\n");
                    fwrite($classfile,"            return '".$value["desc"]."的字数必须在".$sub[1]."~".$sub[2]."之间';\n");
                    fwrite($classfile,"        }\n");
                }
                //范围比对
                if(isset($value["area"])){
                    fwrite($classfile,"        var area=[\"".implode('","',$value["area"])."\"];\n");
                    fwrite($classfile,"        if(area.indexOf(".$key.")==-1){\n");
                    fwrite($classfile,"            return '".$value["desc"]."不在指定的范围内';\n");
                    fwrite($classfile,"        }\n");
                }
                //正则比对
                if(isset($value["regex"])){
                    fwrite($classfile,"        var reg=".$value["regex"].";\n");
                    fwrite($classfile,"        var patt=new RegExp(reg);\n");
                    fwrite($classfile,"        if(!patt.test(".$key.")){\n");
                    fwrite($classfile,"            return '".$value["desc"]."的格式不符合规定';\n");
                    fwrite($classfile,"        }\n");
                }
                fwrite($classfile,"        ".$table["class"].".".$key."=".$key.".replace(/\'/g,\"&apos;\");\n");
                fwrite($classfile,"    },\n");
            }
            //UseForm
            fwrite($classfile,"    UseForm: function (elem) {\n");
            fwrite($classfile,"        for (var key in ".$table["class"].") {\n");
            fwrite($classfile,"            if (typeof ".$table["class"]."[key] == \"function\") {\n");
            fwrite($classfile,"                continue;//过滤方法\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            if (elem.querySelector(\"#\" + key) ==null) {\n");
            fwrite($classfile,"                continue;//过滤不存在表单组件\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            var val = \"\";\n");
            fwrite($classfile,"            if(['INPUT','SELECT','TEXTAREA'].indexOf(elem.querySelector(\"#\"+key).nodeName)>-1){;\n");
            fwrite($classfile,"                val=elem.querySelector(\"#\"+key).value;\n");
            fwrite($classfile,"            };\n");
            fwrite($classfile,"            if(['IMG'].indexOf(elem.querySelector(\"#\"+key).nodeName)>-1){;\n");
            fwrite($classfile,"                val=elem.querySelector(\"#\"+key).src;\n");
            fwrite($classfile,"            };\n");
            fwrite($classfile,"            var res = ".$table["class"]."[\"Set_\" + key](val);\n");
            fwrite($classfile,"            if (res) {\n");
            fwrite($classfile,"                return res;\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"        }\n");
            fwrite($classfile,"        return null;\n");
            fwrite($classfile,"    },\n");
            //CreateFormdata
            fwrite($classfile,"    CreateFormdata: function () {\n");
            fwrite($classfile,"        var postData = new FormData();\n");
            fwrite($classfile,"        for (var key in ".$table["class"].") {\n");
            fwrite($classfile,"            if (typeof ".$table["class"]."[key] == \"function\") {\n");
            fwrite($classfile,"                continue;//过滤方法\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            if (".$table["class"]."[key] == null) {\n");
            fwrite($classfile,"                  continue;\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            postData.append(key, ".$table["class"]."[key]);\n");
            fwrite($classfile,"        }\n");
            fwrite($classfile,"        return postData;\n");
            fwrite($classfile,"    },\n");
            //CreateJson
            fwrite($classfile,"    CreateJson: function () {\n");
            fwrite($classfile,"        var json = {};\n");
            fwrite($classfile,"        for (var key in ".$table["class"].") {\n");
            fwrite($classfile,"            if (typeof ".$table["class"]."[key] == \"function\") {\n");
            fwrite($classfile,"                continue;//过滤方法\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            if (".$table["class"]."[key] == null) {\n");
            fwrite($classfile,"                  continue;\n");
            fwrite($classfile,"            }\n");
            fwrite($classfile,"            json[key] = ".$table["class"]."[key];\n");
            fwrite($classfile,"        }\n");
            fwrite($classfile,"        return json;\n");
            fwrite($classfile,"    }\n");

            fwrite($classfile,"}\n");
            fclose($classfile);
        }
        return __CLASS__;
    }
}
?>